package org.flowcircuit.model.board

import _root_.org.flowcircuit.appearance.{Style, DrawContext}
import coordinates.Pos

/**
 * A connection between two points on a Board.
 *
 * @author Hans Haggstrom
 */
case class Trace( a : TracePoint, b : TracePoint, width : Float, style : Style ) extends  CircuitElement {

  override protected def onBoardChanged() {
    if (board != null)  add
    else remove
  }

  def render(context: DrawContext) = {
    val start = a.pos
    val end = b.pos
    context.line( start, end, width, style )
  }

  def isInside(queryPos: Pos, context: DrawContext) = {
    val start = a.pos
    val end = b.pos
    context.isInsideLine( queryPos, start, end, width, style )
  }

  private def add {
    a.traces.add( this )
    b.traces.add( this )
  }

  private def remove {
    a.traces.remove( this )
    b.traces.remove( this )
  }

}